package com.example.sample.activity;

import android.app.Activity;
import android.graphics.Rect;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.example.app.BaseActivity;

public class ListViewScrollActivity extends BaseActivity {
	
	private static final String TAG = "ListViewScrollActivity";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		ListView lv = new ListView(this);
		lv.setAdapter(new BaseAdapter() {
			
			@Override
			public View getView(int position, View convertView, ViewGroup parent) {
				TextView tv = (TextView) convertView;
				if (tv == null){
					tv = new TextView(ListViewScrollActivity.this);
				}
				tv.setText("位置" + position);

				return tv;
			}
			
			@Override
			public long getItemId(int position) {
				// TODO Auto-generated method stub
				return 0;
			}
			
			@Override
			public Object getItem(int position) {
				// TODO Auto-generated method stub
				return null;
			}
			
			@Override
			public int getCount() {
				return 50;
			}
		});
		lv.setOnScrollListener(new OnScrollListener() {
			
			@Override
			public void onScrollStateChanged(AbsListView view, int scrollState) {
				Log.i(TAG, "onScrollStateChanged " + scrollState + " scrolly="+ view.getScrollY());
				
				// 判断滚动到底部
                int lastVisiblePosition = view.getLastVisiblePosition();

                if (lastVisiblePosition == (view.getCount() - 1)) {//当最后一项可见时

                    Rect rect = new Rect();
                    View last_child = view.getChildAt(lastVisiblePosition - view.getFirstVisiblePosition());// 获取listview当前展示的最后一个item
                    boolean visible = last_child.getGlobalVisibleRect(rect);// 获取last_child可见部分的rect
                    //当最后一项完全展开时
                    if (visible && last_child.getHeight() == (rect.bottom - rect.top)) {
                    	Log.i(TAG, "到底了！！！！！！！！！！！ ");
                    }
                }
			}
			
			@Override
			public void onScroll(AbsListView view, int firstVisibleItem,
					int visibleItemCount, int totalItemCount) {
				Log.i(TAG, "onScroll " + firstVisibleItem + " " + visibleItemCount + " " + totalItemCount);
			}
		});		
		setContentView(lv);
	}
}
